package com.lee.boot.head.first.demo.redis.service.impl;

import com.lee.boot.head.first.demo.redis.entity.Owner;
import com.lee.boot.head.first.demo.redis.mapper.OwnerMapper;
import com.lee.boot.head.first.demo.redis.service.OwnerService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class OwnerServiceImpl implements OwnerService {

    @Autowired
    OwnerMapper ownerMapper;

    @Override
    @CachePut(cacheNames = "redisCache", key = "'owner:'+#result.id")
    public Owner insertOwner(Owner owner) {
        ownerMapper.insertOwner(owner);
        return owner;
    }

    @Override
    public Owner getOneByName(String name) {
        return ownerMapper.getOneByName(name);
    }

    @Override
    @Cacheable(cacheNames = "redisCache", key = "'owner:'+#id")
    public Owner getOwner(Long id) {
        return ownerMapper.getOwner(id);
    }

    @Override
    @Cacheable(cacheNames = "redisCache", key = "'owners'")
    public List<Owner> listOwner() {
        return ownerMapper.listOwner();
    }

    @Override
    @CacheEvict(cacheNames = "redisCache", key = "'owner:'+#id")
    public void deleteOwner(Long id) {
        ownerMapper.deleteOwner(id);
    }
}
